package com.hy;


import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.io.*;
import java.sql.SQLException;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

@RunWith(Parameterized.class)
public class TriangleTest {
    private double a, b, c;
    private double expected;
    public static Triangle triangle;

    @BeforeClass
    public static void beforeClass() {
        System.out.println("@BeforeClass代码开始运行");
        triangle = new Triangle();
    }

    //数据库传参和文件传参内部都是String封装所以这里要保持一致
    public TriangleTest(String a, String b, String c, String expected) {
        this.a = Double.parseDouble(a);
        this.b = Double.parseDouble(b);
        this.c = Double.parseDouble(c);
        this.expected = Double.parseDouble(expected);
    }

    /*
    //Collection<Object>类型是Object所以这里必须是Object类型，此时构造方法要是用这个
    public TriangleTest(Object a, Object b, Object c, Object expected) {
        this.a = Double.parseDouble(a.toString());
        this.b = Double.parseDouble(b.toString());
        this.c = Double.parseDouble(c.toString());
        this.expected = Double.parseDouble(expected.toString());
    }*/


   /* @Parameterized.Parameters
    public static Collection<Object> parameters() {
        Object[][] objects = {{3, 0, 0, 9}, {2, 2, 2, 4}, {1, 0, 0, 4},
                {4, 0, 0, 16}, {5, 0, 0, 30}};
        return Arrays.asList(objects);
    }*/


    /*@Parameterized.Parameters
    public static List<String[]> data() throws IOException {
        File file = new File("src/com/hy/lib/paramter.txt");
        FileInputStream fileInputStream = new FileInputStream(file);
        InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "utf-8");
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        List<String[]> list = new ArrayList<>();
        String record;
        while ((record = bufferedReader.readLine()) != null) {
            String[] spilt = record.split(",");
            list.add(spilt);
            System.out.println(Arrays.toString(spilt));
        }
        return list;
    }*/

    @Parameterized.Parameters
    public static Object[][] data() throws IOException, SQLException, ClassNotFoundException {
        return new JdbcTriangle().getCon("src/com/hy/lib/mys.txt");
    }

    @Test
    public void TestArea() {

        try {
            Assert.assertTrue(triangle.TestTrue(a, b, c));
            System.out.println("可以构成三角形");
        } catch (Exception e) {

            Assert.assertEquals("无法构成三角形", e.getMessage());
            System.out.println("无法构成三角形");
        }


    }

    @Test
    public void testPerimeter() {
        System.out.println("边长为" + a + "," + b + "," + c + "时，三角形周长：" + triangle.perimeter(a, b, c));
        Assert.assertEquals(expected, triangle.perimeter(a, b, c), 1);
    }

    @Test
    public void testArea() {
        System.out.println("三角形边长为" + a + "," + b + "," + c + "时，三角形面积：" + triangle.area(a, b, c));
        Assert.assertEquals(expected, triangle.perimeter(a, b, c), 1);
    }
}

